package cn.tedu.csmall.cart.webapi.service.impl;

import cn.tedu.csmall.cart.service.ICartService;
import cn.tedu.csmall.cart.webapi.mapper.CartMapper;
import cn.tedu.csmall.commons.pojo.cart.dto.CardAddDTO;
import cn.tedu.csmall.commons.pojo.cart.model.Cart;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@DubboService
@Service
@Slf4j
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public void cartAdd(CardAddDTO cardAddDTO) {
        //创建Cart对象
        Cart cart = new Cart();
        // 工具类直接将cartAddDTO中的同名属性赋值给cart
        BeanUtils.copyProperties(cardAddDTO,cart);
        //运行持久层方法新增到数据库
        cartMapper.insertCart(cart);
        log.info("新增购物车商品成功!{}",cart);
    }

    @Override
    public void deleteUserCart(String userId, String commodityCode) {
        //调用删除购物车商品的持久层方法
        cartMapper.deleteCartByUserIdAndCommodityCode(userId,commodityCode);
        log.info("删除购物车商品完成!");
    }
}
